package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.Cart;
import com.fbee.smartcommunities.entity.Goods;
import com.fbee.smartcommunities.entity.OrderGoods;
import com.fbee.smartcommunities.mapper.GoodsMapper;
import com.fbee.smartcommunities.mapper.OrderGoodsMapper;
import com.fbee.smartcommunities.service.CartService;
import com.fbee.smartcommunities.service.OrderGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.service.UserService;
import com.fbee.smartcommunities.utils.CustomizeException;
import com.fbee.smartcommunities.vo.OrderGoodsVO;
import com.fbee.smartcommunities.vo.OrderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单商品关系表 服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-27
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements OrderGoodsService {

    @Resource
    private CartService cartService;

    @Resource
    private UserService userService;

    @Resource
    private OrderGoodsMapper orderGoodsMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public boolean add(Long orderId, Long goodsId, Integer count) {
        this.save(new OrderGoods(orderId,goodsId,count,0));
        // 将购物车状态置为在订单之中
        cartService.update(new UpdateWrapper<Cart>().eq("goods_id",goodsId).set("state",1));
        return true;
    }

    @Override
    public List<OrderVO> getOrders(Long uid) {
        return orderGoodsMapper.getOrders(uid);
    }

    @Override
    @Transactional
    public Boolean trade(Long from, Long to, Double count, Long id, Integer sum, Long goodsId, HttpServletRequest request) {
        // 转账
        userService.transfer(from,to,count,request,"购买商品,消费金额: ");
        // 更新订单状态
        if (!this.update(new UpdateWrapper<OrderGoods>().eq("id", id).set("state",1))) {
            throw new CustomizeException("支付异常");
        }
        // 更新商品销量和库存
        Goods goods = goodsMapper.selectById(goodsId);
        try {
            goodsMapper.update(null, new UpdateWrapper<Goods>().eq("id", goodsId).set("sales", goods.getSales() + sum).set("store", goods.getStore() - sum));
        } catch (Exception e){
            throw new CustomizeException("当前商品库存不足请删除该订单");
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean delete(Long id) {
        return orderGoodsMapper.deleteById(id) > 0;
    }

    @Override
    public IPage<OrderGoodsVO> getOrdersByCid(Long openId, String key, Integer pageNo, Integer pageSize) {
        return orderGoodsMapper.getOrdersByCid(openId,new Page<>(pageNo,pageSize),key);
    }
}
